// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
const PuzzleManager = require('PuzzleManager')

cc.Class({
    extends: cc.Component,

    properties: {
        mirrorFrame: {
            type: [cc.SpriteFrame],
            default: [],
            tooltip: '镜面的可能情况'
        },
        buttonFrame: {
            type: [cc.SpriteFrame],
            default: [],
            tooltip: '按钮精灵的可能情况'
        },
        pm: {
            type: PuzzleManager,
            default:null,
            tooltip:'解谜管理器'
        },
        mirror: cc.Sprite,
        index:0
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.photo = this.pm.getMirrorPhoto(this.index);
        this.spriteCom = this.getComponent(cc.Sprite);
        this.mirror.spriteFrame = this.mirrorFrame[this.photo]
    },

    start () {

    },

    onBeginContact: function (contact, selfCollider, otherCollider) {
        if (otherCollider.tag == 8) {
            //与主角相碰
            this.spriteCom.spriteFrame = this.buttonFrame[1];
            if (!this.pm.checkMirrorsOK()) {
                var next = this.photo + 1;
                if (next > 5) next-=6; 
                this.changePhoto(next);
            }
        }
    },

    onEndContact: function (contact, selfCollider, otherCollider) {
        if (otherCollider.tag == 8) {
            //主角移开
            this.spriteCom.spriteFrame = this.buttonFrame[0];
            AudioManager.playSe('按钮', 0.9);
        }
    },

    changePhoto (photo) {
        this.photo = photo;
        this.mirror.spriteFrame = this.mirrorFrame[photo]
        this.pm.setMirror(this.index, photo)
    },
    // update (dt) {},
});
